/**
 * 
 */
package com.orange.dbhelp.dialect.sqlserver;

import com.orange.dbhelp.dialect.PageSqlParser;
import com.orange.dbhelp.util.ArrayUtils;

/**
 * @author gaogs
 *
 * 2016-7-19
 */
public class SqlserverPageSqlParser implements PageSqlParser {

	/* (non-Javadoc)
	 * @see com.orange.dbhelp.dialect.PageSqlParser#getPageSql(java.lang.String, boolean)
	 */
	@Override
	public String getPageSql(String sql, boolean hasOffset,int startIndex, int pageSize) {
		if (hasOffset) {
            return " SELECT TOP "+pageSize+" pagesql.* FROM  ( "+sql+" ) as pagesql WHERE pagesql.pid  NOT IN  (SELECT TOP "+pageSize*startIndex+" pagesql1.pid FROM ("+sql+") as pagesql1 )" ;
        } else {
            return "SELECT TOP "+pageSize+" pagesql.* FROM  ( "+sql+" ) as pagesql";
        }
	}

	/* (non-Javadoc)
	 * @see com.orange.dbhelp.dialect.PageSqlParser#getCountingSql(java.lang.String)
	 */
	@Override
	public String getCountingSql(String sql) {
		 return "select count(1) from ( " + sql + ") as pagecount";
	}

	/* (non-Javadoc)
	 * @see com.orange.dbhelp.dialect.PageSqlParser#attachPageParam(java.lang.Object[], boolean, int, int)
	 */
	@Override
	public Object[] attachPageParam(Object[] params, boolean hasOffset,
			int startIndex, int pageSize) {
		 /*if (hasOffset) {
	            params = ArrayUtils.add(params, startIndex + pageSize);
	            params = ArrayUtils.add(params, startIndex);
	        } else {
	            params = ArrayUtils.add(params, startIndex + pageSize);
	        }*/
	        return params;
	}

}
